package com.ruoyi.api.controller;

import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.bo.AppMyOrderBo;
import com.ruoyi.business.app.bo.AppOrderEvaluateBo;
import com.ruoyi.business.app.vo.AppMyOrderVo;
import com.ruoyi.business.domain.HcxMerchantOrder;
import com.ruoyi.business.service.IHcxMerchantOrderService;
import com.ruoyi.business.service.IHcxOrderEvaluateService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;


@Api(tags = "我的--->我的订单相关接口")
@AllArgsConstructor
@RestController
@RequestMapping("/api/order")
public class MyOrderController extends BaseController {
    private final IHcxOrderEvaluateService orderEvaluateService;
    private final IHcxMerchantOrderService iHcxMerchantOrderService;

    private final RedisCache redisCache;


    @ApiOperation("查询我的订单列表")
    @GetMapping("/my/list")
    @Login
    public TableDataInfo<AppMyOrderVo> list(AppMyOrderBo bo) {
        return iHcxMerchantOrderService.queryAppPageList(bo);
    }


    @ApiOperation("对某个订单进行评价")
    @PostMapping("/my/evaluate")
    @Login
    @RepeatSubmit
    public AjaxResult evaluate(@RequestBody AppOrderEvaluateBo bo) {
        boolean evaluate = orderEvaluateService.evaluate(bo);
        return toAjax(evaluate);
    }

    @RepeatSubmit
    @ApiOperation("对某个订单进行删除")
    @PostMapping("/my/delete")
    @Login
    public AjaxResult delete(@ApiParam("订单id") @RequestParam Long orderId) {
        boolean evaluate = iHcxMerchantOrderService.deleteMyorder(orderId);
        return toAjax(evaluate);
    }

    @ApiOperation("查询我的订单详情")
    @GetMapping("/my/detail")
    @Login
    public AjaxResult<AppMyOrderVo> detail(@ApiParam("订单id") @RequestParam Long orderId) {
        AppMyOrderVo appMyOrderVo = iHcxMerchantOrderService.getVoById(orderId,AppMyOrderVo.class);
        // 订单有效期
        String orderValid = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + Constants.HCXORDERVALID);
        // 订单过期时间
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE,Integer.valueOf(orderValid));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String guoqi = sdf.format(nowTime.getTime());
        appMyOrderVo.setOrderExpirTime(guoqi);
        return AjaxResult.success("success",appMyOrderVo);
    }

    @RepeatSubmit
    @ApiOperation("对某个订单进行取消")
    @PostMapping("/my/cancle")
    @Login
    public AjaxResult cancle(@ApiParam("订单id") @RequestParam Long orderId,@ApiParam("取消原因(传dictLabel的值)") @RequestParam String  calcelReason) {
        HcxMerchantOrder hcxMerchantOrder = iHcxMerchantOrderService.getById(orderId);
        hcxMerchantOrder.setStatus(Constants.FOUR);
        hcxMerchantOrder.setCalcelReason(calcelReason);
        boolean result = iHcxMerchantOrderService.updateById(hcxMerchantOrder);
        return toAjax(result);
    }
}
